<?php

namespace app\index\controller;

use app\admin\library\Auth;
use app\common\controller\Frontend;
use EasyWeChat\Factory;
use app\admin\model\Admin;
use app\common\controller\Api;

class Royalty extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

   /**
     * 公共
     * @author gzy
     * @date   2018年8月12日 09:15:00
     */
    public function _initialize(){
        parent::_initialize();
        $Api = new Api();
        //获取默认值
        $this->config = $Api->getConfig($Api->getDefault('Royalty'));
        $this->auth = \app\admin\library\Auth::instance();
        $this->model = new \app\admin\model\Admininfo;
        $this->Info = new \app\admin\model\finance\extract\Info;
        //退出
        //print_r($this->auth->logout());die; 
        //模拟登陆
      // $this->auth->IndexLogin('GaoZhenYu');
        //判断权限
        //print_r($this->auth->check('see/index'));die;
    }

   
    public function index(){
       $this->auth->logout();
       // 检测是否需要验证登录
       return redirect($this->request->domain().'/index/royalty/login');
        
    }  
    public function lists($year = NULL)
    {
        header('Access-Control-Allow-Credentials:true');
        if(!$year) $year = date('Y');
        if (!$this->auth->isLogin()){
            return redirect($this->request->domain().'/index/royalty/login');
        }
        $userid =  \think\Session::get('admin')['userid'];
        $username =  \think\Session::get('admin')['username'];
        //TODO 后期做搜索用
        $start_time = $year.'-01-01 00:00:00';
        $end_time = $year.'-12-31 23:59:59';
  
        $where = "b.start_time between '".$start_time."' and '".$end_time."' and a.`userid` = '".$userid."'";
        $data = \think\Db::name('admin_info')->alias('a')
                        ->field("b.id,b.start_time,b.end_time,b.real_hair")
                        ->join('finance_extract_info b','a.userid = b.userid')
                        ->where($where)
                        ->order('b.start_time asc')
                        ->select();
        
        $this->view->assign('data',$data);
        $this->view->assign('year',$year);
        $this->view->assign('username',$username);
        return $this->view->fetch();
        
    }   
    public function detail($ids = null)
    {
        header('Access-Control-Allow-Credentials:true');
       
        if (!$this->auth->isLogin()){
            return redirect($this->request->domain().'/index/royalty/login');
        }
        if(!$ids) $this->error(__('您要查找的不存在'));
  
        $userid =  \think\Session::get('admin')['userid'];
        $username =  \think\Session::get('admin')['username'];
        $where = " a.`userid` = '".$userid."' and b.id = '".$ids."'";
        $info = \think\Db::name('admin_info')->alias('a')
                        ->field("b.*")
                        ->join('finance_extract_info b','a.userid = b.userid')
                        ->where($where)
                        ->find();
          if(!$info) $this->error(__('您要查找的不存在'));
        //print_r(excelArray(2));die;
        $this->view->assign('info',$info);
        $this->view->assign('ino_array',excelArray(2));

        return $this->view->fetch();
        
    }
    public function login(){
        $app = Factory::work($this->config);
        $token = $app->access_token->getToken();//获取企业微信token
        $code = $this->request->request('code');
        $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$token['access_token']}&code={$code}";
        $user = \fast\Http::get($url);
        if(!empty($user)) $user = json_decode($user,true);
        
        if($user['errcode'] == 0){
            if(isset($user['UserId'])){
                if(!$this->auth->IndexLogin($user['UserId'])){
                    $this->error(__('您的账户不存在，请联系管理员'));
                }else{
                    //print_r(\think\Session::get('admin')['userid']);die;
                    return redirect($this->request->domain().'/index/royalty/lists');
                }
            }
        }else{
            return redirect($this->request->domain().'/index/royalty/oauth');
        }
    }
   public function oauth(){
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->config['corp_id']."&redirect_uri=".$this->config['oauth']['callback']."&response_type=code&scope=".$this->config['oauth']['scopes']."&agentid=".$this->config['agent_id']."&state=STATE#wechat_redirect";
        return redirect($url);
    }
  

}
